package demo.db.hbase.utils;

import kong.unirest.Util;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class CommonUtils {

    public static Map<String, Map<String, Map<Long, String>>> parseMap3(
            Map<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map) {
        if (map == null || map.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<String, Map<String, Map<Long, String>>> result = new HashMap<>(map.size());
        for (Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry : map.entrySet()) {
            Map<String, Map<Long, String>> tmpResult = new HashMap<>();
            for (Entry<byte[], NavigableMap<Long, byte[]>> tmpEntry : entry.getValue().entrySet()) {
                Map<Long, String> tt = tmpEntry.getValue().entrySet().stream().map(e -> {
                    return new KeyValuePair<>(e.getKey(), Bytes.toString(e.getValue()));
                }).collect(Collectors.toMap(KeyValuePair::getKey, KeyValuePair::getValue));
                tmpResult.put(Bytes.toString(tmpEntry.getKey()), tt);
            }
            result.put(Bytes.toString(entry.getKey()), tmpResult);
        }

        return result;
    }

    public static Map<String, Map<String, String>> parseMap2(Map<byte[], NavigableMap<byte[], byte[]>> map) {
        if (map == null || map.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<String, Map<String, String>> result = new HashMap<>(map.size());
        for (Entry<byte[], NavigableMap<byte[], byte[]>> entry : map.entrySet()) {
            result.put(Bytes.toString(entry.getKey()), CommonUtils.parseMap(entry.getValue()));
        }
        return result;
    }

    public static Map<String, String> parseMap(Map<byte[], byte[]> map) {
        if (map == null || map.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<String, String> result = new HashMap<>(map.size());
        for (Entry<byte[], byte[]> entry : map.entrySet()) {
            result.put(Bytes.toString(entry.getKey()), Bytes.toString(entry.getValue()));
        }
        return result;
    }

    @Test
    public void testBasicAuth() {
        String username = "arthas";
        String password = "123456";
        String basicAuth = Util.toBasicAuthValue(username, password);
        System.out.println(basicAuth);

        String a = "YWRtaW46YWRtaW4=";
        byte[] b = Base64.getDecoder().decode(a);
        System.out.println(new String(b));
    }

}
